<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="140px"
    >
      <el-form-item label="分配方案名称" prop="schemeName">
        <el-input
          v-model="queryParams.schemeName"
          placeholder="请输入分配方案名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="佣金计算输入" prop="calcField">
        <el-select
          v-model="queryParams.calcField"
          clearable
          placeholder="请选择"
          @keyup.enter.native="handleQuery"
          style="width: 215px"
        >
          <!-- <el-option key="Receipts" label="实收" value="Receipts" />
          <el-option key="Receivable" label="应收" value="Receivable" />
          <el-option key="Profit" label="利润" value="Profit" /> -->
          <el-option
            v-for="dict in dict.type.agt_calc_field"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="方案整体分成比例" prop="tscale">
        <el-input
          v-model="queryParams.tscale"
          placeholder="请输入方案整体分成比例"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联商品" prop="pdcScope">
        <el-select
          v-model="queryParams.pdcScope"
          clearable
          placeholder="请选择"
          @keyup.enter.native="handleQuery"
          style="width: 215px"
        >
          <!-- <el-option key="1" label="全部商品" value="1" />
          <el-option key="0" label="指定商品" value="0" />
          <el-option key="2" label="充值" value="2" />
          <el-option key="3" label="旅行体验券" value="3" />
          <el-option key="4" label="拓店" value="4" />
          <el-option key="5" label="发展团队" value="5" /> -->
          <el-option
            v-for="dict in dict.type.agt_pdc_scope"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="首购复购" prop="firstTrade">
        <el-select
          v-model="queryParams.firstTrade"
          clearable
          placeholder="请选择"
          @keyup.enter.native="handleQuery"
          style="width: 215px"
        >
          <!-- <el-option key="0" label="不区分" value="0" />
          <el-option key="1" label="首购" value="1" />
          <el-option key="2" label="复购" value="2" /> -->
          <el-option
            v-for="dict in dict.type.agt_first_trade"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="返佣是否含客户自己" prop="includeSelf">
        <el-select
          v-model="queryParams.includeSelf"
          clearable
          placeholder="请选择"
          @keyup.enter.native="handleQuery"
          style="width: 215px"
        >
          <!-- <el-option key="0" label="否" value="0" />
          <el-option key="1" label="是" value="1" /> -->
          <el-option
            v-for="dict in dict.type.agt_include_self"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分佣人数" prop="assignCnt">
        <el-input
          v-model="queryParams.assignCnt"
          placeholder="请输入分佣人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否有效" prop="enabled">
        <el-select
          v-model="queryParams.includeSelf"
          clearable
          placeholder="请选择"
          @keyup.enter.native="handleQuery"
          style="width: 215px"
        >
          <!-- <el-option key="0" label="无效" value="0" />
          <el-option key="1" label="有效" value="1" /> -->
          <el-option
            v-for="dict in dict.type.sys_enabled"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['api:scheme:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['api:scheme:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['api:scheme:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExport"
          v-hasPermi="['api:scheme:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="schemeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column
        label="分配方案名称"
        align="center"
        prop="schemeName"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column label="返佣类别" align="center" prop="schemeType">
        <template slot-scope="scope">
          <!-- <span>{{
            scope.row.schemeType == "1"
              ? "层级返佣"
              : scope.row.schemeType == "2"
              ? "级差返佣"
              : scope.row.schemeType == "3"
              ? "等级返佣"
              : scope.row.schemeType == "4"
              ? "货款解冻"
              : ""
          }}</span> -->
          <dict-tag
            :options="dict.type.agt_scheme_type"
            :value="scope.row.schemeType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="佣金计算输入"
        align="center"
        prop="calcField"
        min-width="120"
      >
        <template slot-scope="scope">
          <!-- <span>{{
            scope.row.calcField == "Receipts"
              ? "实收"
              : scope.row.calcField == "Receivable"
              ? "应收"
              : scope.row.calcField == "Profit"
              ? "利润"
              : ""
          }}</span> -->
          <dict-tag
            :options="dict.type.agt_calc_field"
            :value="scope.row.calcField"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="方案整体分成比例"
        align="center"
        prop="tscale"
        min-width="140"
      />
      <el-table-column
        label="分佣计算方式"
        align="center"
        prop="calcType"
        min-width="120"
      >
        <template slot-scope="scope">
          <!-- <span>{{
            scope.row.calcType == "1"
              ? "按比例"
              : scope.row.calcType == "2"
              ? "固定金额"
              : ""
          }}</span> -->
          <dict-tag
            :options="dict.type.agt_calc_type"
            :value="scope.row.calcType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="关联商品"
        align="center"
        prop="pdcScope"
        min-width="100"
      >
        <template slot-scope="scope">
          <!-- <span>{{
            scope.row.pdcScope == "1"
              ? "全部商品"
              : scope.row.pdcScope == "0"
              ? "指定商品"
              : scope.row.pdcScope == "2"
              ? "充值"
              : scope.row.pdcScope == "3"
              ? "旅行体验券"
              : scope.row.pdcScope == "4"
              ? "拓店"
              : ""
          }}</span> -->
          <dict-tag
            :options="dict.type.agt_pdc_scope"
            :value="scope.row.pdcScope"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="交易支付方式 "
        align="center"
        prop="payType"
        min-width="120"
      >
        <template slot-scope="scope">
          <!-- <span>{{
            scope.row.payType == "0"
              ? "不区分"
              : scope.row.payType == "1"
              ? "现金支付"
              : scope.row.payType == "2"
              ? "余额支付"
              : ""
          }}</span> -->
          <dict-tag
            :options="dict.type.agt_pay_type"
            :value="scope.row.payType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="首购复购"
        align="center"
        prop="firstTrade"
        min-width="120"
      >
        <template slot-scope="scope">
          <!-- <span>{{
            scope.row.firstTrade == "0"
              ? "不区分"
              : scope.row.firstTrade == "1"
              ? "首购"
              : scope.row.firstTrade == "2"
              ? "复购"
              : ""
          }}</span> -->
          <dict-tag
            :options="dict.type.agt_first_trade"
            :value="scope.row.firstTrade"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="返佣是否含客户自己"
        align="center"
        prop="includeSelf"
        min-width="160"
      >
        <template slot-scope="scope">
          <!-- <span>{{
            scope.row.includeSelf == "0"
              ? "否"
              : scope.row.includeSelf == "1"
              ? "是"
              : ""
          }}</span> -->
          <dict-tag
            :options="dict.type.agt_include_self"
            :value="scope.row.includeSelf"
          />
        </template>
      </el-table-column>
      <el-table-column label="分佣人数" align="center" prop="assignCnt" />
      <el-table-column
        label="是否有效"
        align="center"
        prop="enabled"
        min-width="120"
      >
        <template slot-scope="scope">
          <!-- <span>{{
            scope.row.enabled == "0"
              ? "无效"
              : scope.row.enabled == "1"
              ? "有效"
              : ""
          }}</span> -->
          <dict-tag
            :options="dict.type.sys_enabled"
            :value="scope.row.enabled"
          />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createDt" />
      <el-table-column label=" 账套号" align="center" prop="sysAccount" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        min-width="120"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['api:scheme:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['api:scheme:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分佣方案配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="分配方案名称" prop="schemeName">
          <el-input
            v-model="form.schemeName"
            placeholder="请输入分配方案名称"
          />
        </el-form-item>
        <el-form-item label="佣金计算输入" prop="calcField">
          <!-- <el-input
            v-model="form.calcField"
            placeholder="请输入佣金计算输入 【trade 中的 实收 Receipts  应收 Receivable  利润 Profit】"
          /> -->
          <el-select
            v-model="form.calcField"
            clearable
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.agt_calc_field"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="方案整体分成比例" prop="tscale">
          <el-input
            v-model="form.tscale"
            placeholder="请输入方案整体分成比例"
          />
        </el-form-item>
        <el-form-item label="关联商品" prop="pdcScope">
          <!-- <el-input
            v-model="form.pdcScope"
            placeholder="请输入关联商品 【1 默认全部商品 0 指定商品 2 充值,3.旅行体验券,4.拓店,5发展团队】"
          /> -->
          <el-select
            v-model="form.pdcScope"
            clearable
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.agt_pdc_scope"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="首购复购" prop="firstTrade">
          <!-- <el-input
            v-model="form.firstTrade"
            placeholder="请输入首购复购 【0 不区分 1 首购 2 复购】"
          /> -->
          <el-select
            v-model="form.firstTrade"
            clearable
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.agt_first_trade"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="返佣是否含客户自己" prop="includeSelf">
          <!-- <el-input
            v-model="form.includeSelf"
            placeholder="请输入返佣是否含客户自己 【0 否 1 是】"
          /> -->
          <el-select
            v-model="form.includeSelf"
            clearable
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.agt_include_self"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="分佣人数" prop="assignCnt">
          <el-input v-model="form.assignCnt" placeholder="请输入分佣人数" />
        </el-form-item>
        <el-form-item label="是否有效" prop="enabled">
          <!-- <el-input
            v-model="form.enabled"
            placeholder="请输入是否有效【0无效，1有效】"
          /> -->
          <el-select
            v-model="form.enabled"
            clearable
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.sys_enabled"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="创建时间" prop="createDt">
          <el-input v-model="form.createDt" placeholder="请输入创建时间" />
        </el-form-item>
        <el-form-item label="数据更新版本" prop="version">
          <el-input v-model="form.version" placeholder="请输入数据更新版本" />
        </el-form-item> -->
        <el-form-item label=" 备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入 备注" />
        </el-form-item>
        <!-- <el-form-item label=" 账套号" prop="sysAccount">
          <el-input v-model="form.sysAccount" placeholder="请输入 账套号" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listScheme,
  getScheme,
  delScheme,
  addScheme,
  updateScheme,
} from "@/api/djk/platformMgr/subcommissionSet";

export default {
  name: "Scheme",
  dicts: [
    "sys_enabled",
    "agt_calc_field",
    "agt_pdc_scope",
    "agt_first_trade",
    "agt_include_self",
    "agt_scheme_type",
    "agt_calc_type",
    "agt_pay_type",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分佣方案配置表格数据
      schemeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schemeName: null,
        schemeType: null,
        calcField: null,
        tscale: null,
        calcType: null,
        pdcScope: null,
        payType: null,
        firstTrade: null,
        includeSelf: null,
        assignCnt: null,
        enabled: null,
        createDt: null,
        version: null,
        sysAccount: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        schemeName: [
          { required: true, message: "分配方案名称不能为空", trigger: "blur" },
        ],
        schemeType: [
          {
            required: true,
            message: "返佣类别不能为空",
            trigger: "change",
          },
        ],
        calcField: [
          {
            required: true,
            message: "佣金计算输入不能为空",
            trigger: "blur",
          },
        ],
        calcType: [
          {
            required: true,
            message: "分佣计算方式不能为空",
            trigger: "change",
          },
        ],
        pdcScope: [
          {
            required: true,
            message: "关联商品不能为空",
            trigger: "blur",
          },
        ],
        payType: [
          {
            required: true,
            message: "交易支付方式不能为空",
            trigger: "change",
          },
        ],
        firstTrade: [
          {
            required: true,
            message: "首购复购不能为空",
            trigger: "blur",
          },
        ],
        includeSelf: [
          {
            required: true,
            message: "返佣是否含客户自己不能为空",
            trigger: "blur",
          },
        ],
        assignCnt: [
          { required: true, message: "分佣人数不能为空", trigger: "blur" },
        ],
        createDt: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        version: [
          { required: true, message: "数据更新版本不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询分佣方案配置列表 */
    getList() {
      this.loading = true;
      listScheme(this.queryParams).then((response) => {
        // this.schemeList = response.rows;
        let list = [];
        response.rows.forEach((item) => {
          list.push(item.apiAgtSchemeVO);
        });
        this.schemeList = list;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        schemeName: null,
        schemeType: null,
        calcField: null,
        tscale: null,
        calcType: null,
        pdcScope: null,
        payType: null,
        firstTrade: null,
        includeSelf: null,
        assignCnt: null,
        enabled: null,
        createDt: null,
        version: null,
        remark: null,
        sysAccount: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分佣方案配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getScheme(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分佣方案配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateScheme(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addScheme(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除分佣方案配置编号为"' + ids + '"的数据项？')
        .then(function () {
          return delScheme(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "api/scheme/export",
        {
          ...this.queryParams,
        },
        `scheme_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
